package com.qst.system.controller;

import com.qst.common.core.controller.BaseController;
import com.qst.common.core.domain.AjaxResult;
import com.qst.common.core.domain.entity.Cities;
import com.qst.common.core.domain.entity.Provinces;
import com.qst.common.core.page.TableDataInfo;
import com.qst.system.domain.Company;
import com.qst.system.domain.CompanyDetails;
import com.qst.system.domain.vo.CompanyDetailsVo;
import com.qst.system.service.ICitiesService;
import com.qst.system.service.ICompanyDetailsService;
import com.qst.system.service.ICompanyService;
import com.qst.system.service.IProvincesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

@RestController
@RequestMapping("/system/details")
public class CompanyDetailsController extends BaseController {

    @Autowired
    private ICompanyDetailsService companyDetailsService;

    @Autowired
    private IProvincesService provincesService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private ICitiesService citiesService;

    @PreAuthorize("@ss.hasPermi('system:details:list')")
    @GetMapping("/list")
    public TableDataInfo list(CompanyDetailsVo companyDetailsVo) {
        startPage();
        List<CompanyDetailsVo> list = companyDetailsService.selectCompanyDetailsList(companyDetailsVo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:details:list')")
    @GetMapping("/pro")
    public AjaxResult getProvinces() {
        // 调用服务层方法查询所有省份数据（根据你的实际业务调整）
        List<Provinces> provincesList = provincesService.selectProvincesList(null);
        System.out.println(provincesList);
        // 返回数据给前端（使用你项目中的统一响应类，如AjaxResult）
        return AjaxResult.success(provincesList);
    }

    @PreAuthorize("@ss.hasPermi('system:details:list')")
    @GetMapping("/companies/{cId}" )
    public AjaxResult selectCompanyDetailsById( @PathVariable("cId") Long cId){
        CompanyDetailsVo companyDetailsVo = companyDetailsService.selectCompanyDetailsById(cId);
        return AjaxResult.success(companyDetailsVo);
    }

    @PreAuthorize("@ss.hasPermi('system:details:add')")
    @GetMapping("/companyName")
    public AjaxResult getCompanyName(){
        return AjaxResult.success(companyService.selectCompanyList(null));
    }

    @PreAuthorize("@ss.hasPermi('system:details:add')")
    @PostMapping
    public AjaxResult add(@RequestBody CompanyDetailsVo companyDetailsVo) {

        System.out.println("接收到的企业ID（co_Id）" + companyDetailsVo.getCoId());
        return toAjax(companyDetailsService.insertCompanyDetails(companyDetailsVo));
    }

    @PreAuthorize("@ss.hasPermi('system:details:edit')")
    @GetMapping("/{cId}")
    public AjaxResult selectCompanyDetailsBycId(@PathVariable("cId") Long cId) {
        CompanyDetailsVo companyDetailsVo = companyDetailsService.selectCompanyDetailsById(cId);

        return AjaxResult.success(companyDetailsVo);
    }

    @PreAuthorize("@ss.hasPermi('system:details')")
    @PutMapping
    public AjaxResult edit(@RequestBody CompanyDetailsVo companyDetailsVo) {
        System.out.println("==========================进入");
        System.out.println(companyDetailsVo.toString());
        return toAjax(companyDetailsService.updateCompanyDetails(companyDetailsVo));
    }

    @PreAuthorize("@ss.hasPermi('system:details:remove')")
    @DeleteMapping("/{cIds}")
    public AjaxResult remove(@PathVariable String cIds) {
        try {
            // 处理单个ID情况
            if (!cIds.contains(",")) {
                return toAjax(companyDetailsService.deleteCompanyDetailsById(Long.parseLong(cIds)));
            }

            // 处理批量ID情况
            String[] idStrs = cIds.split(",");
            Long[] ids = new Long[idStrs.length];
            for (int i = 0; i < idStrs.length; i++) {
                ids[i] = Long.parseLong(idStrs[i].trim());
            }
            return toAjax(companyDetailsService.deleteCompanyDetailsByIds(ids));
        } catch (NumberFormatException e) {
            return AjaxResult.error("删除失败，ID格式不正确");
        } catch (Exception e) {
            return AjaxResult.error("删除失败：" + e.getMessage());
        }
    }

    @GetMapping("/comName/{coid}")
    public AjaxResult getDetailsCompanyName(@PathVariable Long coid){
        CompanyDetailsVo companyDetailsVo = companyDetailsService.selectCompanyDetailsById(coid);
        return AjaxResult.success(companyDetailsVo.getCompanyName());
    }
}
